# ============================================================== # It is recommended to test the script on a local machine for its purpose and effects. # ManageEngine Endpoint Central will not be responsible for any # damage/loss to the data/setup based on the behavior of the script. # Description: Script to repair and restart OneDrive client which is used to synchronize files and folders. OneDrive will attempt to perform a synchronization after the restart. # Parameters: # Remarks: # The script uses Powershell cmdlets for scheduling which is not present on Windows 7 and earlier, Windows Server 2008 R2 and earlier # The script has to be deployed as Computer Configuration # The Script will stop OneDrive client, if running # Configuration Type - Computer # ============================================================== # OneDrive executable paths $OneDrivePaths = @( "C:\Program Files\Microsoft OneDrive\OneDrive.exe", "$env:LOCALAPPDATA\Microsoft\OneDrive\OneDrive.exe" ) # Find available OneDrive executable path $OneDriveExe = $OneDrivePaths | Where-Object { Test-Path $_ } | Select-Object -First 1 if (-not $OneDriveExe) { Write-Host "OneDrive is not installed on this system." exit } # Function to create and trigger a scheduled task function Create-And-Trigger-ScheduledTask { param ( [string]$TaskName, [string]$Arguments ) $trigger = New-ScheduledTaskTrigger -Once -At (Get-Date).AddSeconds(1) $action = New-ScheduledTaskAction -Execute $OneDriveExe -Argument $Arguments $settings = New-ScheduledTaskSettingsSet -AllowStartIfOnBatteries -DontStopIfGoingOnBatteries Register-ScheduledTask -Action $action -Trigger $trigger -TaskName $TaskName -RunLevel Limited -Force -Settings $settings Start-ScheduledTask -TaskName $TaskName Write-Host "$TaskName has been triggered." } # Check if OneDrive is running and stop it if necessary $OneDriveRunning = Get-Process -Name "OneDrive" -ErrorAction SilentlyContinue if ($OneDriveRunning) { Start-Process $OneDriveExe -ArgumentList "/shutdown" Write-Host "OneDrive has been stopped." } else { Write-Host "OneDrive is not running. Skipping stop." } # Wait before proceeding Start-Sleep -Seconds 10 # Schedule the reset task if OneDrive is not running Write-Host "Scheduling task to reset OneDrive with /reset /background..." Create-And-Trigger-ScheduledTask -TaskName "OneDriveResetTask" -Arguments "/reset /background" # Wait before scheduling the restart Start-Sleep -Seconds 10 # Check if OneDrive is running; if not, schedule the restart task $OneDriveRunning = Get-Process -Name "OneDrive" -ErrorAction SilentlyContinue if (-not $OneDriveRunning) { Write-Host "OneDrive is not running. Scheduling task to restart OneDrive with /background..." Create-And-Trigger-ScheduledTask -TaskName "OneDriveRestartTask" -Arguments "/background" } else { Write-Host "OneDrive is already running. No restart needed." } # Wait before cleaning up tasks Start-Sleep -Seconds 10 # Clean up by removing the scheduled tasks after execution $tasksToRemove = @("OneDriveResetTask", "OneDriveRestartTask") foreach ($task in $tasksToRemove) { $taskExists = Get-ScheduledTask -TaskName $task -ErrorAction SilentlyContinue if ($taskExists) { Unregister-ScheduledTask -TaskName $task -Confirm:$false Write-Host "Scheduled task '$task' has been removed." } else { Write-Host "No scheduled task '$task' found." } }